package cn.shop.core.controller.front;


import cn.shop.core.bean.ConsigneeManagement;
import cn.shop.core.bean.UserInfo;
import cn.shop.core.service.ConsigneeManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/adminconsignee/")
public class ConsigneeManagementController {

	@Autowired
	private ConsigneeManagementService cmservice;

	/**
	 * 查询收货人
	 * 
	 * @param session
	 * @param cm
	 * @param model
	 * @return
	 */
	@RequestMapping("list.shtml")
	public String list(HttpSession session, ConsigneeManagement cm, Model model) {

		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		if(cm == null){
			cm = new ConsigneeManagement();
		}
		cm.setUserID(user.getUserId());
		
		model.addAttribute("list", cmservice.getlist(cm));

		return "consignee_list";
	}
	
	/**
	 * 添加
	 * @param session
	 * @param cm
	 * @param model
	 * @return
	 */
	@RequestMapping("add.shtml")
	public String add(HttpSession session, ConsigneeManagement cm, Model model){
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		if(cm == null){
			cm = new ConsigneeManagement();
		}
		cm.setUserID(user.getUserId());
		
		try {
			cmservice.add(cm);
			
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("info", "添加失败!");
		}
		
		
		
		return "consignee_index";
	}
	

	
	/**
	 * 修改
	 * @param session
	 * @param cm
	 * @param model
	 * @return
	 */
	@RequestMapping("update.shtml")
	public String update(HttpSession session, ConsigneeManagement cm, Model model){
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		if(cm == null){
			cm = new ConsigneeManagement();
		}
		cm.setUserID(user.getUserId());
		
		try {
			cmservice.update(cm);
			
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("info", "修改失败!");
		}
		
		return "consignee_index";
	}
	
	
	/**
	 * 修改
	 * @param session
	 * @param cm
	 * @param model
	 * @return
	 */
	@RequestMapping("delete.shtml")
	public String delete(HttpSession session, ConsigneeManagement cm, Model model){
		UserInfo user = (UserInfo)session.getAttribute("userInfo");
		if(cm == null){
			cm = new ConsigneeManagement();
		}
		cm.setUserID(user.getUserId());
		
		try {
			cmservice.delete(cm);
			
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("info", "删除失败!");
		}
		
		return "consignee_index";
	}

}
